package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.ProductService;
import com.atguigu.spzx.model.dto.product.ProductDto;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.vo.common.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/product/product")
@Tag(name = "商品管理")
@CrossOrigin(allowCredentials = "true" , originPatterns = "*" , allowedHeaders = "*")
public class ProductController {
    @Autowired
    private ProductService productService;

    @Operation(summary = "分页")
    @GetMapping("/{page}/{limit}")
    public Result<PageInfo<Product>> findByPage(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit, ProductDto productDto){
        PageInfo<Product> pageInfo=productService.findByPage(page,limit,productDto);
        return Result.ok(pageInfo);
    }

    @Operation(summary = "保存")
    @PostMapping("/save")
    public Result save(@RequestBody Product product){
        productService.save(product);
        return Result.ok(null);
    }

    @Operation(summary = "根据ID查询")
    @GetMapping("/getById/{id}")
    public Result<Product> getById(@PathVariable Long id){
        Product product=productService.getById(id);
        return Result.ok(product);
    }

    @Operation(summary = "修改")
    @PutMapping("/updateById")
    public Result updateById(@RequestBody Product product){
        productService.updateById(product);
        return Result.ok(null);
    }

    @Operation(summary = "删除")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id){
        productService.deleteById(id);
        return Result.ok(null);
    }

    @Operation(summary = "审核")
    @GetMapping("/updateAuditStatus/{id}/{auditStatus}")
    public Result   updateAuditStatus(@PathVariable("id") Long id,@PathVariable("auditStatus") Integer auditStatus){
        productService.updateAuditStatus(id,auditStatus);
        return Result.ok(null);
    }

    @Operation(summary = "上下架")
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable("id") Long id,@PathVariable("status") Integer status){
        productService.updateStatus(id,status);
        return Result.ok(null);
    }

}
